package design.graduation.novelsystem.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import design.graduation.novelsystem.constant.Constant;
import design.graduation.novelsystem.mapper.IncludeBookMapper;
import design.graduation.novelsystem.model.domian.IncludeBook;
import design.graduation.novelsystem.model.dto.DataGridView;
import design.graduation.novelsystem.model.vo.IncludeBookVo;
import design.graduation.novelsystem.service.IncludeBookService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: 洪少尉
 * @Date: 2020/4/18 09:19
 * @Description:
 */
@Service
public class IncludeBookServiceImpl extends ServiceImpl<IncludeBookMapper, IncludeBook> implements IncludeBookService {

	/**
	 * 全查询 +条件查询
	 * 用于回写表单数据
	 *
	 * @param includeBookVo 查询条件
	 * @return DataGridView
	 */
	@Override
	public DataGridView queryAllIncludeBook(IncludeBookVo includeBookVo) {
		IPage<IncludeBook> page = new Page<>(includeBookVo.getPage(), includeBookVo.getLimit());
		QueryWrapper<IncludeBook> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(includeBookVo.getStatus() != null, IncludeBook.COL_STATUS, includeBookVo.getStatus())
				.like(StringUtils.isNotBlank(includeBookVo.getBookName()), IncludeBook.COL_BOOK_NAME, includeBookVo.getBookName())
				.like(StringUtils.isNotBlank(includeBookVo.getAuthor()), IncludeBook.COL_AUTHOR, includeBookVo.getAuthor())
				.ge(includeBookVo.getStartTime() != null, IncludeBook.COL_CREATE_TIME, includeBookVo.getStartTime())
				.le(includeBookVo.getEndTime() != null, IncludeBook.COL_EDIT_TIME, includeBookVo.getEndTime());
		this.baseMapper.selectPage(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}

	/**
	 * 修改上下架状态
	 *
	 * @param includeBook id和status
	 * @return boolean
	 */
	@Override
	public boolean updateStatus(IncludeBook includeBook) {
		includeBook.setEditTime(new Date());
		return this.updateById(includeBook);
	}

	/**
	 * 批量上架
	 *
	 * @param id 上架小说id
	 */
	@Override
	public void batchUp(Integer id) {
		this.updateById(new IncludeBook(id,new Date(),Constant.STATUS_TRUE));
	}

	/**
	 * 批量下架
	 *
	 * @param id 下架小说id
	 */
	@Override
	public void batchDown(Integer id) {
		this.updateById(new IncludeBook(id,new Date(),Constant.STATUS_FALSE));
	}
}


